package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ArchivesDO;
import com.jinzhi.jzweb.service.ArchivesService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-07-21 16:50:07 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/archives")
public class ArchivesController extends BaseController {
	@Autowired
	private ArchivesService archivesService;
	@Autowired
	private UserService userService;
	@Autowired
	private
	@GetMapping()
	@RequiresPermissions("jzweb:archives:archives")
	String Archives(){
		//return "jzweb/archives/archives";

		return "jzweb/archives/archedit";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:archives:archives")
	public Result<Page<ArchivesDO>> list(ArchivesDO archivesDTO){
		// 查询列表数据
		Page<ArchivesDO> page = archivesService.selectPage(getPage(ArchivesDO.class),
					archivesService.convertToEntityWrapper().orderBy("actual_money", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				ArchivesDO obj = (ArchivesDO) page.getRecords().get(i);
		UserDO userDO= userService.selectById(obj.getPerson());
			obj.setPersonNmae(userDO.getName());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@GetMapping("/add")
	@RequiresPermissions("jzweb:archives:add")
	String add(String id,Model model){
		model.addAttribute("schId", id);
		return "jzweb/archivesFollow/add";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:archives:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ArchivesDO archives = archivesService.selectById(id);
		model.addAttribute("archives", archives);
		return "jzweb/archives/edit";
		// return "archivesedit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:archives:add")
	public Result<String> save( ArchivesDO archives){
		archivesService.insert(archives);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:archives:edit")
	public Result<String>  update( ArchivesDO archives){
		boolean update = archivesService.updateById(archives);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:archives:remove")
	public Result<String>  remove( Long id){
		archivesService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:archives:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		archivesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@GetMapping("/archList")
	@RequiresPermissions("jzweb:archives:archList")
	String archList(){
		return "jzweb/archives/archivesFollowList";
	}

}
